//
//  JokeCollectionViewCell.m
//  TrekkingTravel
//
//  Created by dlios on 15/8/21.
//  Copyright (c) 2015年 TrekkingTravel. All rights reserved.
//

#import "JokeCollectionViewCell.h"
#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)

@implementation JokeCollectionViewCell

- (void)dealloc
{
    [_jokeM release];
    [_label release];
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}
- (void)createSubViews
{
//    self.contentView.backgroundColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:0.8];
    //圆角
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 8.0;
    self.layer.borderWidth = 1.0;
    self.layer.borderColor = [[UIColor blackColor] CGColor];
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, WIDTH - 30, HEIGHT)];
    self.label.numberOfLines = 0;
    self.label.backgroundColor = [UIColor clearColor];
    
    [self.contentView addSubview:self.label];
    [_label release];
    
}


- (void)setJokeM:(JokeModel *)jokeM
{
    if (_jokeM != jokeM) {
        [_jokeM release];
        _jokeM = [jokeM retain];
    }
    self.label.text = self.jokeM.text;
    CGFloat height = [self heightForContent:self.label.text];
    //使用中间变量赋值
    CGRect frame = self.label.frame;
    frame.size.height = height;
    self.label.frame = frame;
    [self.myDelegate sendSize:frame];
}
- (CGFloat)heightForContent:(NSString *)content
{
    //参数1.设置计算内容的宽高
    //参数2.设置计算高度模式
    //参数3.设置计算字体大小属性
    //参数4.系统备用参数,设置为nil
    //其中宽度一定与显示内容的label宽度一致,否则计算不准确
    CGSize size = CGSizeMake(WIDTH - 30, 10000);
    //字体大小一定与显示内容的label字体大小一致
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17.0] forKey:NSFontAttributeName];
    CGRect frame = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return frame.size.height;
}
+ (CGFloat)heightForContent:(NSString *)content
{
    //参数1.设置计算内容的宽高
    //参数2.设置计算高度模式
    //参数3.设置计算字体大小属性
    //参数4.系统备用参数,设置为nil
    //其中宽度一定与显示内容的label宽度一致,否则计算不准确
    CGSize size = CGSizeMake(WIDTH - 30, 10000);
    //字体大小一定与显示内容的label字体大小一致
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17.0] forKey:NSFontAttributeName];
    CGRect frame = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return frame.size.height;
}

@end
